package com.vajava.str.model;

import org.apache.commons.lang.builder.ToStringBuilder;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;

@Entity(name = "Statistics")
public class Statistics {
    private Long id;
    private TestFile testFile;
    private int totalCount;
    private int successCount;
    private int failedCount;
    private int ignoreCount;
    private List<Run> runs;
    private RunResult lastResult;

    public Statistics(TestFile testFile) {
        this.testFile = testFile;
        this.runs = new ArrayList<Run>();
    }

    protected Statistics() {
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne(optional = false)
    public TestFile getTestFile() {
        return testFile;
    }

    public void setTestFile(TestFile testFile) {
        this.testFile = testFile;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int count) {
        this.totalCount = count;
    }

    public int getSuccessCount() {
        return successCount;
    }

    public void setSuccessCount(int successCount) {
        this.successCount = successCount;
    }

    public int getFailedCount() {
        return failedCount;
    }

    public void setFailedCount(int failedCount) {
        this.failedCount = failedCount;
    }

    public int getIgnoreCount() {
        return ignoreCount;
    }

    public void setIgnoreCount(int ignoreCount) {
        this.ignoreCount = ignoreCount;
    }

    public RunResult getLastState() {
        return lastResult;
    }

    public void setLastState(RunResult lastResult) {
        this.lastResult = lastResult;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Run.class,
            mappedBy = "statistics")
    public List<Run> getRuns() {
        return runs;
    }

    public void addRun(Run run) {
        getRuns().add(run);
    }

    public void setRuns(List<Run> runs) {
        this.runs = runs;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        return testFile.equals(((Statistics)o).testFile);
    }

    public int hashCode() {
        return testFile.hashCode();
    }

    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
